package com.taobao.analyse.map;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;

import com.taobao.analyse.constants.SkuConstants;
import com.taobao.analyse.constants.SplitConstants;

public class ItemSkuSourceMapper extends MapReduceBase implements
		Mapper<Writable, Text, LongWritable, Text> {

	@Override
	public void map(Writable key, Text value,
			OutputCollector<LongWritable, Text> output, Reporter reporter)
			throws IOException {
		// 分割符为tab
		String line = value.toString();
		String[] skus = StringUtils.splitPreserveAllTokens(line, SplitConstants.TAB);
		if(skus.length < 11){
			return;
		}
		
		String price = skus[SkuConstants.SKU_PRICE];
		String status = skus[SkuConstants.SKU_STATUS];
		String skuId = skus[SkuConstants.SKU_ID];
		String itemId = skus[SkuConstants.SKU_ITEMID];
		if(status.equals("1")){
			try{
				long aid = Long.parseLong(itemId);
				if(StringUtils.isEmpty(itemId)||StringUtils.isEmpty(skuId)||StringUtils.isEmpty(price)){
					return;
				}
				StringBuilder sb = new StringBuilder();
				buildSkuInfo(sb,skuId,price);
				output.collect(new LongWritable(aid), new Text(sb.toString()));
			}catch(Exception e){
				
			}
		}
	}

	private void buildSkuInfo(StringBuilder sb, String skuId, String price) {
		sb.append(skuId).append(SplitConstants.SPLIT_UNDER_BAR).append(price);
	}

}
